package com.ktjy.car.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.car.entity.Complaint;
import com.ktjy.car.mapper.ComplaintMapper;
import com.ktjy.car.service.ComplaintService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Resource
    ComplaintMapper complaintMapper;

    @Override
    public IPage<Complaint> findPageVo(Complaint complaint, Integer pageNo, Integer pageSize) {
        Page<Complaint> page = new Page<>(pageNo, pageSize);
        return complaintMapper.findPage(page);
    }

    @Override
    public IPage<Complaint> selectPageByName(String brandName, String problem, String carType, Integer current, Integer size) {
        Page<Complaint> page = new Page<>(current, size);
        QueryWrapper<Complaint> queryWrapper = new QueryWrapper<>();
        
        // 构建查询条件
        if (brandName != null && !brandName.isEmpty()) {
            queryWrapper.like("b.brandName", brandName);
        }
        if (problem != null && !problem.isEmpty()) {
            queryWrapper.like("c.problem", problem);
        }
        if (carType != null && !carType.isEmpty()) {
            queryWrapper.like("c.carType", carType);
        }

            queryWrapper.orderByDesc("c.complaintsTime");
        
        return complaintMapper.searchPage(page, queryWrapper);
    }

    @Override
    public int delete(int id) {
        return complaintMapper.deleteById(id);
    }

    @Override
    public int insert(Complaint complaint) {
        // 验证必填字段
        if (complaint.getBrandId() == null || 
            complaint.getProblem() == null || 
            complaint.getProblem().trim().isEmpty()) {
            throw new RuntimeException("品牌和问题描述不能为空");
        }
        return complaintMapper.insert(complaint);
    }

    @Override
    public int update(Complaint complaint) {
        if (complaint.getId() == null || complaint.getStatus() == null) {
            throw new RuntimeException("投诉ID和状态不能为空");
        }
        return complaintMapper.update(complaint.getId(), complaint.getStatus());
    }
}